package AccessDB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.fileupload.FileItem;

import model.MaterialEstudo;

public class MaterialEstudoDAO {
	
	private Connection con;
	
	public MaterialEstudoDAO() {
		super();
		con =  ConnectionDB.getInstance().getConnection();
	}
	
	public List<MaterialEstudo> retornaMateriaisEstudoDisciplinaProf(int idDisciplinaProf) throws SQLException{
		
		PreparedStatement stmt = con.prepareStatement("select * from MaterialEstudo where idDisciplinaProf = ? ");
		stmt.setInt(1, idDisciplinaProf);
		ResultSet rs = stmt.executeQuery();
		
		List<MaterialEstudo> materiais = new ArrayList<MaterialEstudo>();
		while(rs.next())
			materiais.add(gerarMaterialEstudo(rs));
		
		return materiais;
	}
	
	private MaterialEstudo gerarMaterialEstudo(ResultSet rs) throws SQLException{
		
		MaterialEstudo mte = new MaterialEstudo();		
			
		mte.setArquivo(rs.getString("caminhoArquivo"));
		mte.setIdMaterial(rs.getInt("idMaterial"));
		mte.setTitulo(rs.getString("titulo"));
		mte.setData(rs.getDate("data"));
		
		return mte;
	}
	
	public boolean inserirArquivoPasta(String caminho, int idDiscProf, String titulo, String data) throws IOException, SQLException{
		

        
        //Inserindo o caminho do arquivo no banco
        
        PreparedStatement stmt = con.prepareStatement("insert into MaterialEstudo values(?,?,?,?)");
        stmt.setInt(1, idDiscProf);
        stmt.setString(2, titulo);
        String str = caminho;
        stmt.setString(3, str);
        stmt.setString(4, data);
        stmt.execute();
       
        return true;
	}

	// novo metodo por Rui
	public void editarTituloMaterial(int id, String novoTitulo) throws SQLException
	{
		PreparedStatement stmt = con.prepareStatement("update MaterialEstudo set titulo = ? where idMaterial = ?");
		stmt.setString(1,novoTitulo);
		stmt.setInt(2, id);
		
		stmt.execute();
	}
	
	//novo Metodo por Rui
	public void removerMaterial(int idMaterial) throws SQLException
	{
		PreparedStatement stmt = con.prepareStatement("delete from MaterialEstudo where idMaterial = ?");
		stmt.setInt(1, idMaterial);
		
		stmt.execute();
	}
}